home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / dev / c / pqueue.lha / PQueue.c next >
C/C++ Source or Header  |  1994-10-03  |  2KB  |  152 lines

  1. /* Unbounded PQueue implementation */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "PQueue.h"
  7.  
  8. struct Node
  9. {
  10.     ELEMENT         elt;
  11.     PRIORITY        pty;
  12.     PTR_S_NODE            next;
  13. };
  14.  
  15. struct PQueueDescr
  16. {
  17.     long            currSize;
  18.     PTR_S_NODE        head;
  19. };
  20.  
  21. /**********************************************************/
  22.  
  23. PTR_S_PQUEUEDESCR Create(PTR_S_PQUEUEDESCR pq)
  24. {
  25.     if (pq=(PTR_S_PQUEUEDESCR) malloc(sizeof(S_PQUEUEDESCR)))
  26.     {
  27.         pq->currSize=0;
  28.         pq->head=NULL;
  29.         return(pq);
  30.     }
  31.     else
  32.         return(pq);
  33. }
  34.  
  35. /**********************************************************/
  36.  
  37. void Destroy(PTR_S_PQUEUEDESCR pq)
  38. {
  39.     ELEMENT e;
  40.     
  41.     while (IsEmpty(pq)==0)
  42.         Dequeue(pq,&e);
  43.     free(pq);
  44. }
  45.  
  46.  
  47. /**********************************************************/
  48.  
  49. BOOLEAN IsEmpty(PTR_S_PQUEUEDESCR pq)
  50. {
  51.     return(pq->currSize==0);
  52. }
  53.  
  54. /**********************************************************/
  55.  
  56. long Size(PTR_S_PQUEUEDESCR pq)
  57. {
  58.     return(pq->currSize);
  59. }
  60.  
  61. /**********************************************************/
  62.  
  63. BOOLEAN Enqueue(PTR_S_PQUEUEDESCR pq,ELEMENT elt,PRIORITY py)
  64. {
  65.     PTR_S_NODE currPtr,tempPtr,helpPtr;
  66.     int isOnTop;
  67.     
  68.     if (currPtr=(PTR_S_NODE) malloc(sizeof(S_NODE)))
  69.     {
  70.         currPtr->elt=elt;
  71.         currPtr->pty=py;
  72.         helpPtr=pq->head;
  73.         tempPtr=pq->head;
  74.         isOnTop=1;
  75.         while((tempPtr!=NULL) && (currPtr->pty<tempPtr->pty))
  76.         {
  77.             isOnTop=0;
  78.             helpPtr=tempPtr;
  79.             tempPtr=tempPtr->next;
  80.         }
  81.         if(IsEmpty(pq))
  82.         {
  83.             pq->head=currPtr;
  84.             currPtr->next=NULL;
  85.         }
  86.         else
  87.         {
  88.             if(currPtr->pty<tempPtr->pty)
  89.             {
  90.                 helpPtr->next=currPtr;
  91.                 currPtr->next=tempPtr;
  92.             }
  93.             else
  94.             {
  95.                 currPtr->next=tempPtr;
  96.                 if(isOnTop)
  97.                     pq->head=currPtr;
  98.                 else
  99.                     helpPtr->next=currPtr;
  100.             }
  101.         }
  102.         pq->currSize++;
  103.         return(1);
  104.     }
  105.     else
  106.         return(0);
  107. }
  108.  
  109. /**********************************************************/
  110.  
  111. BOOLEAN Dequeue(PTR_S_PQUEUEDESCR pq,PTR_ELEMENT elt)
  112. {
  113.     PTR_S_NODE currPtr;
  114.     
  115.     if(IsEmpty(pq)==0)
  116.     {
  117.         currPtr=pq->head;
  118.         *elt=currPtr->elt;
  119.         pq->head=currPtr->next;
  120.         free(currPtr);
  121.         pq->currSize--;
  122.         return(1);
  123.     }
  124.     else
  125.         return(0);
  126. }
  127.  
  128. /**********************************************************/
  129.  
  130. BOOLEAN IsMemoryAvailable(void)
  131. {
  132.     PTR_S_NODE currPtr;
  133.     
  134.     if((currPtr=((PTR_S_NODE) malloc(sizeof(S_NODE))))!=NULL)
  135.     {
  136.         free(currPtr);
  137.         return(1);
  138.     }
  139.     else
  140.         return(0);
  141. }
  142.  
  143. /**********************************************************/
  144.  
  145. void GetRelease(PTR_CHAR release)
  146. {
  147.     strcpy(release," D1.0 I1.0");
  148. }
  149.  
  150. /**********************************************************/
  151.  
  152.